package com.fresh.wx.utils;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import com.fresh.wx.utils.RequestHandler;

public class XMLUtil {

	
	/** 
	* 解析xml,返回第一级元素键值对。如果第一级元素有子节点，则此节点的值是子节点的xml数据。 
	* @param strxml 
	* @return 
	* @throws JDOMException 
	* @throws IOException 
	*/ 
	public static Map doXMLParse(String strxml)  { 
		
	    Map m = new HashMap();
		
		try {
		    strxml = strxml.replaceFirst("encoding=\".*\"", "encoding=\"UTF-8\"");
	
		    if(null == strxml || "".equals(strxml)) {
		        return null;
		    }
	
		    InputStream in = new ByteArrayInputStream(strxml.getBytes("UTF-8"));
		    SAXBuilder builder = new SAXBuilder();
		    Document doc = builder.build(in);
		    Element root = doc.getRootElement();
		    List list = root.getChildren();
		    Iterator it = list.iterator();
		    while(it.hasNext()) {
		        Element e = (Element) it.next();
		        String k = e.getName();
		        String v = "";
		        List children = e.getChildren();
		        if(children.isEmpty()) {
		            v = e.getTextNormalize();
		        } else {
		            v = XMLUtil.getChildrenText(children);
		        }
	
		        m.put(k, v);
		    }
	
		    //关闭流
		    in.close();
	    
		}catch(Exception e){
			
		}

	    return m;
	}

	/**
	 * 获取子结点的xml
	 * @param children
	 * @return String
	 */
	public static String getChildrenText(List children) {
	    StringBuffer sb = new StringBuffer();
	    if(!children.isEmpty()) {
	        Iterator it = children.iterator();
	        while(it.hasNext()) {
	            Element e = (Element) it.next();
	            String name = e.getName();
	            String value = e.getTextNormalize();
	            List list = e.getChildren();
	            sb.append("<" + name + ">");
	            if(!list.isEmpty()) {
	                sb.append(XMLUtil.getChildrenText(list));
	            }
	            sb.append(value);
	            sb.append("</" + name + ">");
	        }
	    }

	    return sb.toString();
	}
	
	/**
	 * 校验签名
	 * @param map
	 * @return
	 */
	public static boolean checkSign(Map<String, Object> map){
		
		if(map!=null&&map.size()>0){
			
			String sign = map.get("sign").toString();
			
			SortedMap<String, String> packageParams = new TreeMap<String, String>();
			
			//便利结果
			Iterator iter = map.entrySet().iterator();
			while (iter.hasNext()) {
			    Map.Entry entry = (Map.Entry) iter.next();
			    String key = entry.getKey().toString();
			    String val = entry.getValue().toString();
			    
			    if(!key.equals("sign")){
			    	packageParams.put(key, val);	
			    }
			    
			}
			
			RequestHandler reqHandler = new RequestHandler(null, null);
			reqHandler.init("wx643fdb439f53163a", "f1f11cf992eec600da01bc9e6702c960", "8407ef2d67cc4526972c1a5aaaaf5001");
			
			// 创建md5摘要
			String newSign = reqHandler.createSign(packageParams);
			if(sign.equals(newSign)){
				return true;
			}
		}
		
		return false;
		
	}
	
}
